<?php
namespace Admin\Model;
use Think\Model;
class MemberModel extends Model 
{
	protected $insertFields = array('username','password','cpassword','chkcode','must_click');
	protected $updateFields = array('nick_name','email','id','username');
    //添加会员的验证表单规则
	protected $_validate = array(
	    array('must_click','require','必须同意注册协议！',1,'regex',1),
		array('username', 'require', '用户名不能为空！', 1, 'regex', 3),
		array('username', '1,30', '用户名的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('password', 'require', '密码不能为空！', 1, 'regex', 3),
		array('password', '1,32', '密码的值最长不能超过 32 个字符！', 1, 'length', 3),
        array('cpassword','password','两次密码输入不一致！',1,'confirm',3),
        array('username','','用户名已存在！',1,'unique',3),
		array('chkcode','require','验证不能为空！',1),
        array('chkcode','check_verify','验证码不正确！',1,'callback'),

	);

    public $_update_validate = array(
        array('nick_name', 'require', '昵称不能为空！', 1, 'regex', 3),
    );

    //登录表单的验证规则
    public $_login_validate = array(
        array('username','require','用户名不能为空！',1),
        array('password','require','密码不能为空！',1),
        array('chkcode','require','验证码不能为空！',1),
        array('chkcode','check_verify','验证码不正确',1,'callback'),
    );

    /**验证验证码是否正确**/
    public function check_verify($code,$id=''){
        $verify = new \Think\Verify();
        return $verify->check($code,$id);
    }
    public function login($needPassword = TRUE)
    {
        //从模型中获取用户名和密码
        $username = $this->username;
        $password = $this->password;

        //查询用户是否存在
        $user = $this->field('id,username,password,jifen,nick_name')
            ->where(array(
                'username' => array('eq', $username),
            ))->find();
        if ($user) {
            if ($needPassword){
                if ($user['password'] == $password) {
                    //登录成功存SESSION
                    session('m_id', $user['id']);
                    session('m_username', $user['username']);
                    session('m_nickname',$user['nick_name']);

                    $mlModel = D('member_level');
                    $levelId = $mlModel->field('id')->where(array(
                        'jifen_bottom' => array('elt', $user['jifen']),
                        'jifen_top' => array('egt', $user['jifen']),
                    ))->find();
                    session('level_id', $levelId['id']);
                    $cartModel = D('Home/Cart');
                    $cartModel->moveDataToDb();
                    return true;
                } else {
                    $this->error = '密码不正确！';
                    return false;
                }
        } else {
            //登录成功存SESSION
            session('m_id', $user['id']);
            session('m_username', $user['username']);
            session('m_nickname',$user['nick_name']);

            $mlModel = D('member_level');
            $levelId = $mlModel->field('id')->where(array(
                'jifen_bottom' => array('elt', $user['jifen']),
                'jifen_top' => array('egt', $user['jifen']),
            ))->find();
            session('level_id', $levelId['id']);
            $cartModel = D('Home/Cart');
            $cartModel->moveDataToDb();
            return true;
        }
    }
        else
        {
            $this->error = '用户名不存在！';
            return false;
        }

    }
	public function search($pageSize = 2)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($username = I('get.username'))
			$where['username'] = array('like', "%$username%");
		if($password = I('get.password'))
			$where['password'] = array('like', "%$password%");
		if($jifen = I('get.jifen'))
			$where['jifen'] = array('eq', $jifen);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['face']) && $_FILES['face']['error'] == 0)
		{
			$ret = uploadOne('face', 'Member', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['face'] = $ret['images'][0];
				$data['big_face'] = $ret['images'][1];
				$data['mid_face'] = $ret['images'][2];
				$data['sm_face'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
        $id = $option['where']['id'];
		if(isset($_FILES['face']) && $_FILES['face']['error'] == 0)
		{
			$ret = uploadOne('face', 'Member', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['face'] = $ret['images'][0];
				$data['big_face'] = $ret['images'][1];
				$data['mid_face'] = $ret['images'][2];
				$data['sm_face'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
            $oldLogo = $this->field('face,big_face,mid_face,sm_face')->find($id);
            unlink('./Public/Uploads/' . $oldLogo['face']);
            unlink('./Public/Uploads/' . $oldLogo['big_face']);
            unlink('./Public/Uploads/' . $oldLogo['mid_face']);
            unlink('./Public/Uploads/' . $oldLogo['sm_face']);
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('face,big_face,mid_face,sm_face')->find($option['where']['id']);
		deleteImage($images);
	}
    public function logout(){
        session(null);
    }
}